package service

import (
	"context"
	"fmt"
	__ "network_srv/basic/proto"
	"network_srv/handler/models"
)

type Server struct {
	__.UnimplementedNetworkServer
}

// 登录
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user models.User
	user.Mobile = in.Mobile
	user.Password = in.Password
	err := user.GetUserByMobile()
	if err != nil {
		return nil, fmt.Errorf("获取用户信息失败")
	}
	if user.Id == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}

	return &__.LoginResp{
		UserId: int64(user.Id),
	}, nil
}

// 视频列表
func (s *Server) VideoList(_ context.Context, in *__.VideoListReq) (*__.VideoListResp, error) {
	//VideohideList := config.Rdb.Get(config.Ctx, "videoList").Val()
	//var video models.Video
	//var VideoList []*__.VideoList
	//if VideohideList == "" {
	//
	//	list, err := video.VideoList()
	//	if err != nil {
	//		return nil, fmt.Errorf("视频列表展示失败")
	//	}
	//	for _, v := range list {
	//		VideoList = append(VideoList, &__.VideoList{
	//			Title:     v.Title,
	//			CoverUrl:  v.CoverUrl,
	//			VideoType: v.VideoType,
	//			Hot:       int64(v.Hot),
	//		})
	//	}
	//	marshal, _ := json.Marshal(list)
	//	config.Rdb.Set(config.Ctx, "videoList", marshal, time.Minute*5)
	//} else {
	//	json.Unmarshal([]byte(VideohideList), &video)
	//}

	return &__.VideoListResp{}, nil
}

// 视频热度推荐
func (s *Server) VideoHotList(_ context.Context, in *__.VideoHotListReq) (*__.VideoHotListResp, error) {
	//var user models.User
	//err := user.GetUserLogin()
	//if err != nil {
	//	return nil, fmt.Errorf("获取用户信息失败")
	//}
	//if user.Status == 2 {
	//	return nil, fmt.Errorf("该账号未登录，禁止访问")
	//}
	var video models.Video
	video.VideoType = in.VideoType
	list, err := video.VideoList(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, fmt.Errorf("视频列表展示失败")
	}
	var videoHotList []*__.VideoHotList
	for _, v := range list {
		videoHotList = append(videoHotList, &__.VideoHotList{
			Title:     v.Title,
			CoverUrl:  v.CoverUrl,
			VideoType: v.VideoType,
			Hot:       int64(v.Hot),
		})
	}
	return &__.VideoHotListResp{
		List: videoHotList,
	}, nil
}

// 视频热度推荐
func (s *Server) CourseDetail(_ context.Context, in *__.CourseDetailReq) (*__.CourseDetailResp, error) {
	var course models.Course
	course.Id = uint(in.Id)
	err := course.CourseDetail()
	if err != nil {
		return nil, fmt.Errorf("获取课程详情失败")
	}
	if course.Id == 0 {
		return nil, fmt.Errorf("该课程不存在")
	}
	//err = course.GetStatus()
	//if err != nil {
	//	return nil, fmt.Errorf("获取课程信息失败")
	//}
	//if course.Status == 2 {
	//	return nil, fmt.Errorf("您没有购买课程无法查看第二页")
	//}
	return &__.CourseDetailResp{
		CoverUrl: course.CoverUrl,
		VideoUrl: course.VideoUrl,
		Price:    float32(course.Price),
		VipPrice: float32(course.VipPrice),
	}, nil
}

// 我的视频
func (s *Server) GetUserMyCollect(_ context.Context, in *__.GetUserMyCollectReq) (*__.GetUserMyCollectResp, error) {
	var collect models.Collect
	collect.UserId = uint(in.UserId)
	err := collect.GetUserMyCollect()
	if err != nil {
		return nil, fmt.Errorf("获取用户收藏失败")
	}
	if collect.Id == 0 {
		return nil, fmt.Errorf("未找到我的收藏")
	}
	return &__.GetUserMyCollectResp{
		Title:    collect.Title,
		CoverUrl: collect.CoverUrl,
		Price:    float32(collect.Price),
		VideoId:  int64(collect.VideoId),
	}, nil
}
